package utility.webmailclient.app.usermanagement.gwt.registration.widget;


import java.util.ArrayList;

import utility.webmailclient.app.usermanagement.gwt.registration.rpc.RPCService;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.RPCServiceAsync;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.callback.GotQuestionListCallback;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.command.RetrieveQuestionList;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.command.RetrieveQuestionListResponse;
import utility.webmailclient.app.usermanagement.gwt.registration.rpc.model.Question;
import utility.webmailclient.app.usermanagement.gwt.registration.widget.common.Utils;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;



public class UserProfileWidget extends Composite{
private FormPanel registrationFormPanel = new FormPanel(); 
private Panel userProfilePanel = new FlowPanel() ;

private Label userNameLabel = new Label();
private TextBox userNameTextBox = new TextBox();
private Label passwordLabel = new Label();

private Label passwordStrengthLabel = new Label();
private HTML passwordStrengthHTML = new HTML("<p>Please use atleast 6 characters for password</p>");

private PasswordTextBox passwordTextBox = new PasswordTextBox();
private Label verifyPasswordLabel=  new Label();
private PasswordTextBox verifyPasswordTextBox = new PasswordTextBox();


private Label firstNameLabel = new Label();
private TextBox firstNameTextBox = new TextBox();;

private Label lastNameLabel = new Label();
private TextBox lastNameTextBox = new TextBox();;
 
private Label primaryQuestionLabel = new Label();;

private ListBox primaryQuestionListBox = new ListBox(); 

private Label otherPrimaryQuestionLabel = new Label();;
private TextBox otherPrimaryQuestionTextBox = new TextBox(); 


private Label secondaryEmailLabel = new Label();
private TextBox secondaryEmailTextBox = new TextBox();

private Button registerButton = new Button();
private Button resetButton  = new Button();

	public UserProfileWidget() {
		super();
		
		Panel userNameHPanel = new HorizontalPanel();
		userNameLabel.setText("Username");
		userNameHPanel.add(userNameLabel);
		userNameHPanel.add(userNameTextBox);
		
		Panel passwordHPanel = new HorizontalPanel();
		passwordLabel.setText("Password");
		passwordStrengthLabel.setText("Password Strength");
		passwordHPanel.add(passwordLabel);
		passwordHPanel.add(passwordTextBox);
		passwordHPanel.add(passwordStrengthLabel);
		passwordHPanel.add(passwordStrengthHTML);
		
		/**
		 * Adding password strength checker to password box
		 * 
		 */
		passwordTextBox.addKeyPressHandler(new KeyPressHandler() {
			
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
				// TODO Auto-generated method stub
				Window.alert("keypressed");
			if(event.getSource() instanceof PasswordTextBox &&event.getSource() == passwordTextBox)
			{
			   String  password = ((PasswordTextBox)event.getSource()).getText();
			   if(password.length() != 0)
			   passwordStrengthHTML.setHTML(Utils.checkPasswordStrength(password).toString());
			   else
				   passwordStrengthHTML.setHTML("");   
			   }
				
			}
		});
		
		passwordTextBox.addKeyboardListener(new KeyboardListener() {
			
			@Override
			public void onKeyUp(Widget arg0, char arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onKeyPress(Widget arg0, char arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onKeyDown(Widget arg0, char arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
		});
		Panel verifyPasswordHPanel = new HorizontalPanel();
		verifyPasswordLabel.setText("Verify Password");
		verifyPasswordHPanel.add(verifyPasswordLabel);
		verifyPasswordHPanel.add(verifyPasswordTextBox);
				
		Panel firstNameHPanel = new HorizontalPanel();
		firstNameLabel.setText("First Name");
		firstNameHPanel.add(firstNameLabel);
		firstNameHPanel.add(firstNameTextBox);
		
		Panel lastNameHPanel = new  HorizontalPanel();
		lastNameLabel.setText("Last Name");
		lastNameHPanel.add(lastNameLabel);
		lastNameHPanel.add(lastNameTextBox);
		
		Panel primaryQuestionHPanel = new HorizontalPanel();
		primaryQuestionLabel.setText("Primary Question");
		primaryQuestionHPanel.add(primaryQuestionListBox);
		
		Panel otherPrimaryQuestionHPanel = new HorizontalPanel();
		otherPrimaryQuestionLabel.setText("Primary Question");
		otherPrimaryQuestionHPanel.add(otherPrimaryQuestionTextBox);

		Panel secondaryEmailHPanel = new HorizontalPanel();
		secondaryEmailLabel.setText("Secondary Email");
		secondaryEmailHPanel.add(secondaryEmailLabel);
		secondaryEmailHPanel.add(secondaryEmailTextBox);
		
		Panel registerHPanel = new HorizontalPanel();
		registerHPanel.add(registerButton);
		registerButton.setText("Register");
		resetButton.setText("Reset");
		registerHPanel.add(registerButton);
		/**
		 * Adding clickhandler for register button
		 */
		registerButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
				// TODO Auto-generated method stub
				Window.alert("Register button clicked");
			}
		});
		
		registerHPanel.add(resetButton);
		Panel userProfileVPanel  = new VerticalPanel();
		
		userProfileVPanel.add(userNameHPanel);
		userProfileVPanel.add(passwordHPanel);
		userProfileVPanel.add(verifyPasswordHPanel);
		userProfileVPanel.add(firstNameHPanel);
		userProfileVPanel.add(lastNameHPanel);
		userProfileVPanel.add(primaryQuestionHPanel);
		userProfileVPanel.add(otherPrimaryQuestionHPanel);
		userProfileVPanel.add(secondaryEmailHPanel);
		userProfileVPanel.add(registerHPanel);
		
		userProfilePanel.add(userProfileVPanel);
		populateQuestion();
		initWidget(userProfilePanel);
		// TODO write primary question populator from DB and internalization
		
	}
	
	private void populateQuestion(){
		//RPC call to retrieve question from backend
		RetrieveQuestionList retrieveQuestionList = new RetrieveQuestionList();
		RPCServiceAsync rPCServiceAsync =  GWT.create(RPCService.class);
		((ServiceDefTarget)rPCServiceAsync).setServiceEntryPoint(GWT.getModuleBaseURL()+"question.rpc");
		
		GotQuestionListCallback gotQuestionListCallback = new GotQuestionListCallback() {
			@Override
			public void onSuccess(RetrieveQuestionListResponse response) {
				// TODO Populate the primaryQuestion Listbox
				Window.alert("Success");
				Window.alert(" No.of Questions Retrived ="+response.getQuestionList().size());
				this.gotQuestion(response.getQuestionList());				
			}

			@Override
			public void onFailure(Throwable error) {
				// TODO Auto-generated method stub
				
				error.printStackTrace();
				Window.alert("Error Occurred");
				this.operationFailed();
			}


			@Override
			public void operationFailed() {
				// TODO Auto-generated method stub
				primaryQuestionListBox.clear();
				primaryQuestionListBox.addItem("Failed to Retrive", "-1");
			}
			
			@Override
			public void gotQuestion(ArrayList<Question> questionList) {
				// TODO Auto-generated method stub
				primaryQuestionListBox.clear();
				primaryQuestionListBox.addItem("Select a Question","0");
				for(Question question: questionList)
					primaryQuestionListBox.addItem(question.getQuestionText(),question.getQuestionId().toString());

				
			}
		}; 

	
		rPCServiceAsync.execute(retrieveQuestionList,gotQuestionListCallback);
		}
	
	
	
}

